﻿using System;
using System.Collections.Generic;
using System.Web;
using Jxym.DBUtility;
using System.IO;
using System.Reflection;
using System.Data.SqlClient;
using Jxym.wlkc2011.Module.CMS.MODEL;

namespace Jxym.wlkc2011.Module.CMS.Public
{
    public class cmsPublic
    {
        /// <summary>
        /// 判断数据信息是否包含子信息
        /// </summary>
        /// <param name="menuId"></param>
        /// 
        /// 
        /// <returns></returns>
        public bool IsChilds(int menuId)
        {
            bool rv = false;
            MsSqlDal<Cms_ContentClass> ccc = new MsSqlDal<Cms_ContentClass>();
            IList<Cms_ContentClass> list = ccc.GetList("ParentID=" + menuId, null, "", 0);
            if (list.Count > 0)
            {
                rv = true;
            }
            return rv;
        }
        /// <summary>
        /// 根据数据库信息类型展示信息到浏览器
        /// </summary>
        /// <param name="info"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static string ViewContent(string sitename, Cms_Content info, string width, string height)
        {
            string path = HttpContext.Current.Server.MapPath("/UpFiles/" + sitename + "/" + info.FileName);

            if (!System.IO.File.Exists(path))
            {
                sitename = "userPhoto";
            }
            string rv = "";
            if (info != null)
            {
                MsSqlDal<Cms_ContentType> cct = new MsSqlDal<Cms_ContentType>();
                Cms_ContentType cctInfo = cct.GetModel(info.ContentTypeID);
                if (cctInfo != null)
                {
                    string divName = "player_" + DateTime.Now.ToFileTime().ToString();

                    if (cctInfo.TypeKey == "URL")
                    {
                        rv = "<a href='" + info.MYURL + "' target='_blank' title='" + info.Title + "'>" + info.Title + "</a>";
                    }
                    else if (cctInfo.TypeKey == "FLV-MP4")
                    {
                        rv = "<div id='" + divName + "' class='jwvideo'>正在努力的加载播放器...</div>" +
                        "<script type=\"text/javascript\" src=\"jwplayer/jwplayer.js\"></script>" +
                        "<script type='text/javascript'>" +
                        "/*jwplayer.key = \"b+mykey\";*/" +
                        "jwplayer('" + divName + "').setup({";

                        if (info.ViewPhoto != "" && info.ViewPhoto != "no.jpg")
                        {
                            rv += "image: '/UpFiles/" + sitename + "/" + info.ViewPhoto + "',";
                        }
                        else
                        {
                            rv += "image: '/jwplayer/preview.jpg',";
                        }

                        rv += (width != "" ? " width:'" + width + "'" : "") + "," +
                            (height != "" ? " height:'" + height + "'" : "") + "," +
                        "file: '/UpFiles/" + sitename + "/" + info.FileName + "'" +
                        "});" +
                        "</script>" +
                        "<a href='jwplayer/jwplayer.flash.swf'></a><a href='jwplayer/preview.jpg'></a><a href='/UpFiles/" + sitename + "/" + info.FileName + "'></a>";
                    }
                    else if (cctInfo.TypeKey == "MP3")
                    {
                        rv = "<div style='text-align:center' id='" + divName + "'><object id='player_" + DateTime.Now.ToFileTime() + "' " + (width != "" ? " width='" + width + "'" : "") + " height='90' classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6' viewastext='VIEWASTEXT' codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715' standby='Loading Microsoft Windows Media Player components...' type='pplication/x-oleobject'>" +
                             "<param name='AutoStart' value='-1' /><!--是否自动播放-->" +
                             "<param name='Balance' value='0' /><!--调整左右声道平衡,同上面旧播放器代码-->" +
                             "<param name='enabled' value='-1' /><!--播放器是否可人为控制-->" +
                             "<param name='url' value='/UpFiles/" + sitename + "/" + info.FileName + "' /><!--播放的文件地址-->" +
                             "<param name='PlayCount' value='1' /><!--播放次数控制,为整数-->" +
                             "<param name='rate' value='1' /><!--播放速率控制,1为正常,允许小数,1.0-2.0-->" +
                             "<param name='currentPosition' value='0' /><!--控件设置:当前位置-->" +
                             "<param name='currentMarker' value='0' /><!--控件设置:当前标记-->" +
                             "<param name='defaultFrame' value='' /><!--显示默认框架-->" +
                             "<param name='invokeURLs' value='0' /><!--脚本命令设置:是否调用URL-->" +
                             "<param name='baseURL' value='' /><!--脚本命令设置:被调用的URL-->" +
                             "<param name='stretchToFit' value='1' /><!--是否按比例伸展-->" +
                             "<param name='volume' value='100' /><!--默认声音大小0%-100%,50则为50%-->" +
                             "<param name='mute' value='0' /><!--是否静音-->" +
                             "<param name='uiMode' value='mini' /><!--播放器显示模式:Full显示全部;mini最简化;None不显示播放控制,只显示视频窗口;invisible全部不显示-->" +
                             "<param name='windowlessVideo' value='0' /><!--如果是0可以允许全屏,否则只能在窗口中查看-->" +
                             "<param name='fullScreen' value='0' /><!--开始播放是否自动全屏-->" +
                             "<param name='enableErrorDialogs' value='-1' /><!--是否启用错误提示报告-->" +
                             "<param name='SAMIStyle' value='value' /><!--SAMI样式-->" +
                             "<param name='SAMILang' value='value' /><!--SAMI语言-->" +
                             "<param name='SAMIFilename' value='value' /><!--字幕ID--></object></div>" +
                             "<a href='/UpFiles/" + sitename + "/" + info.FileName + "'></a>";
                    }
                    else if (cctInfo.TypeKey == "PHOTO")
                    {
                        rv = "<div style='text-align:center' id='" + divName + "'><img alt=\"" + info.Title + "\" src=\"/UpFiles/" + sitename + "/" + info.FileName + "\" ></img></div>";
                    }
                    else if (cctInfo.TypeKey == "WORD" || cctInfo.TypeKey == "PPT" || cctInfo.TypeKey == "PDF")
                    {
                        rv = "<div style='text-align:center' id='" + divName + "'>" +
                        "<script type='text/javascript' src='FlexPaper/js/flexpaper_flash.js'></script>" +
                        "<a id='viewerPlaceHolder' style='" + (width != "" ? "width:" + width + ";" : "") + (height != "" ? "height:" + height + ";" : "") + "display: block'></a>" +
                        "<div class=\"clear\"></div>" +
                        "<script type='text/javascript'>var fp = new FlexPaperViewer('FlexPaper/FlexPaperViewer','viewerPlaceHolder', { config: {SwfFile: escape('/UpFiles/" + sitename + "/" + info.FileName + "')," +
                        "Scale: 1,ZoomTransition: 'easeOut',ZoomTime: 0.5,ZoomInterval: 0.1,FitPageOnLoad: true,FitWidthOnLoad: true,PrintEnabled: false,FullScreenAsMaxWindow: false," +
                        "ProgressiveLoading: true,MinZoomSize: 0.2,MaxZoomSize: 5,SearchMatchAll: true,InitViewMode: 'Portrait',ViewModeToolsVisible: true,ZoomToolsVisible: true," +
                        "NavToolsVisible: true,CursorToolsVisible: true,SearchToolsVisible: true,localeChain: 'zh_CN'}});</script></div>" +
                        "<a href='FlexPaper/js/flexpaper_flash.js'></a><a href='FlexPaper/FlexPaperViewer.swf'></a><a href='/UpFiles/" + sitename + "/" + info.FileName + "'></a>";
                    }
                    else
                    {
                        rv += "<div>" + info.Html + "</div>";
                    }
                }
            }
            return rv;
        }
        /// <summary>
        /// 根据文件类型展示文件到浏览器
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static string ViewContentByFileType(string sitename, string fileName, string viewFileName, string width, string height, bool isSwf)
        {
            string path = HttpContext.Current.Server.MapPath("/UpFiles/" + sitename + "/" + fileName);
            if (!System.IO.File.Exists(path))
            {
                sitename = "userPhoto";
            }

            string rv = "";

            if (fileName != "")
            {
                MsSqlDal<Cms_ContentType> cct = new MsSqlDal<Cms_ContentType>();

                string fileExtName = Path.GetExtension(fileName).ToUpper();

                if (fileExtName == ".FLV" || fileExtName == ".MP4")
                {
                    string divName = "player_" + DateTime.Now.ToFileTime().ToString();
                    rv = "<div id='" + divName + "' class='jwvideo'>正在努力的加载播放器...</div>" +
                    "<script type=\"text/javascript\" src=\"jwplayer/jwplayer.js\"></script>" +
                    "<script type='text/javascript'>" +
                    "/*jwplayer.key = \"b+mykey\";*/" +
                    "jwplayer('" + divName + "').setup({";
                    if (viewFileName != "" && viewFileName != "no.jpg")
                    {
                        rv += "image: '/UpFiles/" + sitename + "/" + viewFileName + "',";
                    }
                    else
                    {
                        rv += "image: '/jwplayer/preview.jpg',";
                    }
                    rv += (width != "" ? " width:'" + width + "'" : "") + "," +
                    (height != "" ? " height:'" + height + "'" : "") + "," +
                    "file: '/UpFiles/" + sitename + "/" + fileName + "'" +
                    "});" +
                    "</script>" +
                    "<a href='jwplayer/jwplayer.flash.swf'></a><a href='jwplayer/preview.jpg'></a><a href='/UpFiles/" + sitename + "/" + fileName + "'></a>";
                }
                else if (fileExtName == ".WMV" || fileExtName == ".AVI" || fileExtName == ".WMA" || fileExtName == ".MP3")
                {
                    rv = "<div style='text-align:center'><object id='player_" + DateTime.Now.ToFileTime() + "' " + (width != "" ? " width='" + width + "'" : "") + " height='" + (fileExtName == "MP3" ? "50px" : height) + "' classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6' viewastext='VIEWASTEXT' codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715' standby='Loading Microsoft Windows Media Player components...' type='pplication/x-oleobject'>" +
                            "<param name='AutoStart' value='-1' /><!--是否自动播放-->" +
                            "<param name='Balance' value='0' /><!--调整左右声道平衡,同上面旧播放器代码-->" +
                            "<param name='enabled' value='-1' /><!--播放器是否可人为控制-->" +
                            "<param name='url' value='/UpFiles/" + sitename + "/" + fileName + "' /><!--播放的文件地址-->" +
                            "<param name='PlayCount' value='1' /><!--播放次数控制,为整数-->" +
                            "<param name='rate' value='1' /><!--播放速率控制,1为正常,允许小数,1.0-2.0-->" +
                            "<param name='currentPosition' value='0' /><!--控件设置:当前位置-->" +
                            "<param name='currentMarker' value='0' /><!--控件设置:当前标记-->" +
                            "<param name='defaultFrame' value='' /><!--显示默认框架-->" +
                            "<param name='invokeURLs' value='0' /><!--脚本命令设置:是否调用URL-->" +
                            "<param name='baseURL' value='' /><!--脚本命令设置:被调用的URL-->" +
                            "<param name='stretchToFit' value='1' /><!--是否按比例伸展-->" +
                            "<param name='volume' value='100' /><!--默认声音大小0%-100%,50则为50%-->" +
                            "<param name='mute' value='0' /><!--是否静音-->" +
                            "<param name='uiMode' value='Full' /><!--播放器显示模式:Full显示全部;mini最简化;None不显示播放控制,只显示视频窗口;invisible全部不显示-->" +
                            "<param name='windowlessVideo' value='0' /><!--如果是0可以允许全屏,否则只能在窗口中查看-->" +
                            "<param name='fullScreen' value='0' /><!--开始播放是否自动全屏-->" +
                            "<param name='enableErrorDialogs' value='-1' /><!--是否启用错误提示报告-->" +
                            "<param name='SAMIStyle' value='value' /><!--SAMI样式-->" +
                            "<param name='SAMILang' value='value' /><!--SAMI语言-->" +
                            "<param name='SAMIFilename' value='value' /><!--字幕ID--></object></div>" +
                            "<a href='/UpFiles/" + sitename + "/" + fileName + "'></a>";
                }
                else if (fileExtName == ".PNG" || fileExtName == ".JPG" || fileExtName == ".GIF" || fileExtName == ".BMP")
                {
                    rv = "<div style='text-align:center'><img src=\"/UpFiles/" + sitename + "/" + fileName + "\" " + (width != "" ? " width='" + width + "'" : "") + " " + (height != "" ? " height='" + height + "'" : "") + "></img></div>";
                }
                else if (fileExtName == ".PDF")
                {
                    rv = "<div style='text-align:center'><object classid=\"clsid:CA8A9780-280D-11CF-A24D-444553540000\" " + (width != "" ? " width='" + width + "'" : "") + " " + (height != "" ? " height='" + height + "'" : "") + " border=\"0\">" +
                 "<param name=\"_Version\" value=\"65539\" />" +
                 "<param name=\"_ExtentX\" value=\"20108\" />" +
                 "<param name=\"_ExtentY\" value=\"10866\" />" +
                 "<param name=\"_StockProps\" value=\"0\" />" +
                 "<param name=\"SRC\" value=\"/UpFiles/" + sitename + "/" + fileName + "\" />" +
                 "<embed id=\"e\" src=\"/UpFiles/" + sitename + "/" + fileName + "\" " + (width != "" ? " width='" + width + "'" : "") + " " + (height != "" ? " height='" + height + "'" : "") + " type=\"application/pdf\" toolbar=\"0\" toolbar=\"false\"></embed>" +
                 "</object></div>" +
                 "<a href='/UpFiles/" + sitename + "/" + fileName + "'></a>";
                }
                else if (fileExtName == ".SWF" && isSwf)
                {
                    rv = "<div style='text-align:center'>" +
                        "<object classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 " + (width != "" ? " width='" + width + "'" : "") + " " + (height != "" ? " height='" + height + "'" : "") + ">" +
                        "<param name='movie' value='/UpFiles/" + sitename + "/" + fileName + "'>" +
                        "<param name='src' value='/UpFiles/" + sitename + "/" + fileName + "'>" +
                        "<param name='wmode' value='window'>" +
                        "<param name='play' value='1'>" +
                        "<param name='loop' value='-1'>" +
                        "<param name='quality' value='high'>" +
                        "<param name='salign' value='lt'>" +
                        "<param name='menu' value='0'>" +
                        "<param name='allowscriptaccess' value='always'>" +
                        "<param name='scale' value='noscale'>" +
                        "<param name='devicefont' value='0'>" +
                        "</object>" +
                        "<a href='/UpFiles/" + sitename + "/" + fileName + "'></a>";
                }
                else if (fileExtName == ".SWF" && !isSwf)
                {
                    rv = "<div style='text-align:center'>" +
                       "<script type='text/javascript' src='FlexPaper/js/flexpaper_flash.js'></script>" +
                       "<a id='viewerPlaceHolder' style='" + (width != "" ? "width:" + width + ";" : "") + (height != "" ? "height:" + height + ";" : "") + "display: block'></a>" +
                       "<script type='text/javascript'>var fp = new FlexPaperViewer('FlexPaper/FlexPaperViewer','viewerPlaceHolder', { config: {SwfFile: escape('/UpFiles/" + sitename + "/" + fileName + "')," +
                       "Scale: 1,ZoomTransition: 'easeOut',ZoomTime: 0.5,ZoomInterval: 0.1,FitPageOnLoad: true,FitWidthOnLoad: true,PrintEnabled: false,FullScreenAsMaxWindow: false," +
                       "ProgressiveLoading: true,MinZoomSize: 0.2,MaxZoomSize: 5,SearchMatchAll: true,InitViewMode: 'Portrait',ViewModeToolsVisible: true,ZoomToolsVisible: true," +
                       "NavToolsVisible: true,CursorToolsVisible: true,SearchToolsVisible: true,localeChain: 'zh_CN'}});</script></div>" +
                       "<a href='FlexPaper/js/flexpaper_flash.js'></a><a href='FlexPaper/FlexPaperViewer.swf'></a><a href='/UpFiles/" + sitename + "/" + fileName + "'></a>";
                }
                else
                {
                    rv = "不能识别的类型:" + fileExtName;
                }
            }
            return rv;
        }

        public static string ViewContentByFileType(string sitename, string fileName, string width, string height, bool isSwf)
        {
            return ViewContentByFileType(sitename, fileName, "", width, height, isSwf);
        }

        #region 获取管理菜单树

        private MsSqlDal<Cms_ContentClass> ccc = new MsSqlDal<Cms_ContentClass>();
        string str = "";
        string children = ",\"children\":";
        public string GetAdminClassTree(string siteName)
        {
            GetTree(siteName, 0);

            return str.TrimStart(children.ToCharArray());
        }
        private void GetTree(string siteName, int parentId)
        {
            IList<Cms_ContentClass> list = ccc.GetList("siteName='" + siteName + "' AND ParentID=" + parentId, null, "Sort", 0);

            if (list.Count > 0)
            {
                str += children + "[";
                foreach (Cms_ContentClass cccInfo in list)
                {
                    str += "{\"id\":\"" + cccInfo.ID + "\",\"text\":\"" + cccInfo.Title + "\",\"leaf\":" + IsLaef(cccInfo.ID);
                    GetTree(siteName, cccInfo.ID);
                    str += "},";
                }
                str = str.TrimEnd(',');
                str += "]";
            }
        }
        private string IsLaef(int id)
        {
            IList<Cms_ContentClass> list = ccc.GetList("ParentID=" + id, null, "Sort", 0);
            if (list.Count > 0)
                return "false";
            return "true";
        }

        #endregion

        /// <summary>
        /// 获取内容类型列表
        /// </summary>
        /// <returns></returns>
        public string Get_ContentTypeList()
        {
            MsSqlDal<Cms_ContentType> msd = new MsSqlDal<Cms_ContentType>();
            IList<Cms_ContentType> list = msd.GetList("");
            return Newtonsoft.Json.JsonConvert.SerializeObject(list);
        }
        /// <summary>
        /// 获取指定编号的内容类型信息
        /// </summary>
        /// <param name="typeId">类型编号</param>
        /// <returns></returns>
        public string Get_ContentTypeById(int typeId)
        {
            MsSqlDal<Cms_ContentType> msd = new MsSqlDal<Cms_ContentType>();
            Cms_ContentType info = msd.GetModel(typeId);
            return Newtonsoft.Json.JsonConvert.SerializeObject(info);
        }
        /// <summary>
        /// 获取所有网站列表
        /// </summary>
        /// <returns></returns>
        public string Get_SiteList()
        {
            MsSqlDal<Cms_Site> msd = new MsSqlDal<Cms_Site>();
            IList<Cms_Site> list = msd.GetList("");
            return Newtonsoft.Json.JsonConvert.SerializeObject(list);
        }
        /// <summary>
        /// 获取指定编号的专题信息
        /// </summary>
        /// <param name="siteId">专题编号</param>
        /// <returns></returns>
        public string Get_SiteById(int siteId)
        {
            MsSqlDal<Cms_Site> msd = new MsSqlDal<Cms_Site>();
            Cms_Site info = msd.GetModel(siteId);
            return Newtonsoft.Json.JsonConvert.SerializeObject(info);
        }

        public static string GetTemplateName(string siteName)
        {
            MsSqlDal<Cms_Site> msd = new MsSqlDal<Cms_Site>();
            Cms_Site info = msd.GetModel("Name='" + siteName + "'", null);
            if (info != null)
            {
                return info.TemplateName;
            }
            else
            {
                return "default";
            }
        }
        public static string GetTemplateHtml(string siteName)
        {
            MsSqlDal<Cms_Site> msd = new MsSqlDal<Cms_Site>();
            Cms_Site info = msd.GetModel("Name='" + siteName + "'", null);
            if (info != null)
            {
                return info.TemplateHtml;
            }
            else
            {
                return "-";
            }
        }
    }
}